package com.me.mvc.model;

import com.google.gson.Gson;
import com.me.mvc.model.entity.Account;
import com.me.mvc.model.entity.Bank;
import com.me.mvc.model.entity.Client;
import com.me.mvc.model.entity.Transaction;

import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.SortedMap;
import java.util.TreeMap;

/**
 * Created by roman on 22.03.15.
 */
public class ModelUtils {

    public static final String CLIENTS_FILE_NAME = "clients.json";
    public static final String BANKS_FILE_NAME = "banks.json";
    public static final String ACCOUNTS_FILE_NAME = "accounts.json";
    public static final String TRANSACTIONS_FILE_NAME = "transactions.json";
    public static final String UTF_8 = "UTF-8";

    private ModelUtils() {
    }

    public static SortedMap<Integer, Client> clientMap = new TreeMap<Integer, Client>();
    public static SortedMap<Integer, Bank> bankMap = new TreeMap<Integer, Bank>();
    public static SortedMap<Integer, Account> accountMap = new TreeMap<Integer, Account>();
    public static SortedMap<Integer, Transaction> transactionMap = new TreeMap<Integer, Transaction>();

    /**
     * init data from scratch
     */
    public static void createContent(){
        clientMap = new TreeMap<Integer, Client>();
        bankMap = new TreeMap<Integer, Bank>();
        accountMap = new TreeMap<Integer, Account>();
        transactionMap = new TreeMap<Integer, Transaction>();

        clientMap.put( 1,new Client(1,"Vladimir Putin"));
        clientMap.put( 2,new Client(2,"Angela Merkel"));
        clientMap.put( 3,new Client(3,"Nikola Sarcosi"));
        clientMap.put( 4,new Client(4,"Boris Colomoiskiy"));
        clientMap.put( 5,new Client(5,"Petr Porochenko"));

        bankMap.put( 1,new Bank(1,"SberBankRossii"));
        bankMap.put( 2,new Bank(2,"RoshenBank"));
        bankMap.put( 3,new Bank(3,"ItalyBank"));
        bankMap.put( 4,new Bank(4,"ПриватБанк"));

        for (int i = 0; i < 4; i++) {
            accountMap.put(i, new Account(i, null, new Date(), new Date(), clientMap.get(i),bankMap.get(i)));
        }

        for (int i = 0; i < 4; i++) {
            if(i==0) continue;
            transactionMap.put(i,new Transaction(i,accountMap.get(i),accountMap.get(4 - i),i*123.34,new Date()));
        }
    }

    public static void saveToFileContent(){
        String clientsJson = readJson(clientMap);
        String bankJson = readJson(bankMap);
        String accountJson = readJson(accountMap);
        String transactionJson = readJson(transactionMap);

        writeToFile(clientsJson, CLIENTS_FILE_NAME);
        writeToFile(bankJson, BANKS_FILE_NAME);
        writeToFile(accountJson, ACCOUNTS_FILE_NAME);
        writeToFile(transactionJson, TRANSACTIONS_FILE_NAME);

    }

    private static void writeToFile(String data, String fileName){
        PrintWriter writer = null;
        try {
            //todo try with class path
            //getClass().getProtectionDomain().getCodeSource().getLocation();
            writer = new PrintWriter(fileName, UTF_8);
            writer.println(data);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }finally {
            writer.close();
        }//todo does not work, why???
    }

    public static String readJson(SortedMap map){
        return new Gson().toJson(map);
    }

    private static String readFromFile(String fileName){

        //todo read from txt
        return null;
    }

    public static void initFromSource(){

        if((clientMap == null|| bankMap == null || accountMap == null || transactionMap == null)
                ||(clientMap.isEmpty() && bankMap.isEmpty() && accountMap.isEmpty() && transactionMap.isEmpty())) {

            clientMap = new Gson().fromJson(readFromFile(CLIENTS_FILE_NAME), SortedMap.class);
            bankMap = new Gson().fromJson(readFromFile(BANKS_FILE_NAME), SortedMap.class);
            accountMap = new Gson().fromJson(readFromFile(ACCOUNTS_FILE_NAME), SortedMap.class);
            transactionMap = new Gson().fromJson(readFromFile(TRANSACTIONS_FILE_NAME), SortedMap.class);

            if((clientMap == null|| bankMap == null || accountMap == null || transactionMap == null)
                    ||(clientMap.isEmpty() && bankMap.isEmpty() && accountMap.isEmpty() && transactionMap.isEmpty())) {

                createContent();
            }

        }
    }

}
